package org.codingq92.hand.aqs;

import java.util.ArrayList;
import java.util.List;

public class TestMyAQSLock {
    public static void main(String[] args) throws InterruptedException {
        long start = System.currentTimeMillis();
        int[] num = new int[]{1000};
        List<Thread> list = new ArrayList<>();
        MyAQSLock lock = new MyAQSLock(false);
        for (int i = 0; i < 100; i++) {
            list.add(new Thread(() -> {
                lock.lock();
                try {
                    for (int i1 = 0; i1 < 10; i1++) {
                        try {
                            Thread.sleep(1);
                        } catch (InterruptedException e) {
                            throw new RuntimeException(e);
                        }
                        num[0]--;
                    }
                } finally {
                    lock.unlock();
                }
            }));
        }
        for (Thread thread : list) {
            thread.start();
        }
        for (Thread thread : list) {
            thread.join();
        }
        System.out.println("程序结束,结果: " + num[0] + ", 耗时: " + (System.currentTimeMillis() - start) + "ms");
    }
}
